home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / sun / jpeg / lib / jdpipe.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-23  |  38.2 KB  |  1,087 lines

  1. /*
  2.  * jdpipe.c
  3.  *
  4.  * Copyright (C) 1991, 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains decompression pipeline controllers.
  9.  * These routines are invoked via the d_pipeline_controller method.
  10.  *
  11.  * There are two basic pipeline controllers.  The simpler one handles a
  12.  * single-scan JPEG file (single component or fully interleaved) with no
  13.  * color quantization or 1-pass quantization.  In this case, the file can
  14.  * be processed in one top-to-bottom pass.  The more complex controller is
  15.  * used when 2-pass color quantization is requested and/or the JPEG file
  16.  * has multiple scans (noninterleaved or partially interleaved).  In this
  17.  * case, the entire image must be buffered up in a "big" array.
  18.  *
  19.  * If you need to make a minimal implementation, the more complex controller
  20.  * can be compiled out by disabling the appropriate configuration options.
  21.  * We don't recommend this, since then you can't handle all legal JPEG files.
  22.  */
  23.  
  24. #include "jinclude.h"
  25.  
  26.  
  27. #ifdef D_MULTISCAN_FILES_SUPPORTED /* wish we could assume ANSI's defined() */
  28. #define NEED_COMPLEX_CONTROLLER
  29. #else
  30. #ifdef QUANT_2PASS_SUPPORTED
  31. #define NEED_COMPLEX_CONTROLLER
  32. #endif
  33. #endif
  34.  
  35.  
  36. /*
  37.  * About the data structures:
  38.  *
  39.  * The processing chunk size for upsampling is referred to in this file as
  40.  * a "row group": a row group is defined as Vk (v_samp_factor) sample rows of
  41.  * any component while downsampled, or Vmax (max_v_samp_factor) unsubsampled
  42.  * rows.  In an interleaved scan each MCU row contains exactly DCTSIZE row
  43.  * groups of each component in the scan.  In a noninterleaved scan an MCU row
  44.  * is one row of blocks, which might not be an integral number of row groups;
  45.  * therefore, we read in Vk MCU rows to obtain the same amount of data as we'd
  46.  * have in an interleaved scan.
  47.  * To provide context for the upsampling step, we have to retain the last
  48.  * two row groups of the previous MCU row while reading in the next MCU row
  49.  * (or set of Vk MCU rows).  To do this without copying data about, we create
  50.  * a rather strange data structure.  Exactly DCTSIZE+2 row groups of samples
  51.  * are allocated, but we create two different sets of pointers to this array.
  52.  * The second set swaps the last two pairs of row groups.  By working
  53.  * alternately with the two sets of pointers, we can access the data in the
  54.  * desired order.
  55.  *
  56.  * Cross-block smoothing also needs context above and below the "current" row.
  57.  * Since this is an optional feature, I've implemented it in a way that is
  58.  * much simpler but requires more than the minimum amount of memory.  We
  59.  * simply allocate three extra MCU rows worth of coefficient blocks and use
  60.  * them to "read ahead" one MCU row in the file.  For a typical 1000-pixel-wide
  61.  * image with 2x2,1x1,1x1 sampling, each MCU row is about 50Kb; an 80x86
  62.  * machine may be unable to apply cross-block smoothing to wider images.
  63.  */
  64.  
  65.  
  66. /*
  67.  * These variables are logically local to the pipeline controller,
  68.  * but we make them static so that scan_big_image can use them
  69.  * without having to pass them through the quantization routines.
  70.  */
  71.  
  72. static int rows_in_mem;        /* # of sample rows in full-size buffers */
  73. /* Work buffer for data being passed to output module. */
  74. /* This has color_out_comps components if not quantizing, */
  75. /* but only one component when quantizing. */
  76. static JSAMPIMAGE output_workspace;
  77.  
  78. #ifdef NEED_COMPLEX_CONTROLLER
  79. /* Full-size image array holding upsampled, but not color-processed data. */
  80. static big_sarray_ptr *fullsize_image;
  81. static JSAMPIMAGE fullsize_ptrs; /* workspace for access_big_sarray() result */
  82. #endif
  83.  
  84.  
  85. /*
  86.  * Utility routines: common code for pipeline controllers
  87.  */
  88.  
  89. LOCAL void
  90. interleaved_scan_setup (decompress_info_ptr cinfo)
  91. /* Compute all derived info for an interleaved (multi-component) scan */
  92. /* On entry, cinfo->comps_in_scan and cinfo->cur_comp_info[] are set up */
  93. {
  94.   short ci, mcublks;
  95.   jpeg_component_info *compptr;
  96.  
  97.   if (cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
  98.     ERREXIT(cinfo->emethods, "Too many components for interleaved scan");
  99.  
  100.   cinfo->MCUs_per_row = (cinfo->image_width
  101.              + cinfo->max_h_samp_factor*DCTSIZE - 1)
  102.             / (cinfo->max_h_samp_factor*DCTSIZE);
  103.  
  104.   cinfo->MCU_rows_in_scan = (cinfo->image_height
  105.                  + cinfo->max_v_samp_factor*DCTSIZE - 1)
  106.                 / (cinfo->max_v_samp_factor*DCTSIZE);
  107.   
  108.   cinfo->blocks_in_MCU = 0;
  109.  
  110.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  111.     compptr = cinfo->cur_comp_info[ci];
  112.     /* for interleaved scan, sampling factors give # of blocks per component */
  113.     compptr->MCU_width = compptr->h_samp_factor;
  114.     compptr->MCU_height = compptr->v_samp_factor;
  115.     compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
  116.     /* compute physical dimensions of component */
  117.     compptr->downsampled_width = jround_up(compptr->true_comp_width,
  118.                        (long) (compptr->MCU_width*DCTSIZE));
  119.     compptr->downsampled_height = jround_up(compptr->true_comp_height,
  120.                         (long) (compptr->MCU_height*DCTSIZE));
  121.     /* Sanity check */
  122.     if (compptr->downsampled_width !=
  123.     (cinfo->MCUs_per_row * (compptr->MCU_width*DCTSIZE)))
  124.       ERREXIT(cinfo->emethods, "I'm confused about the image width");
  125.     /* Prepare array describing MCU composition */
  126.     mcublks = compptr->MCU_blocks;
  127.     if (cinfo->blocks_in_MCU + mcublks > MAX_BLOCKS_IN_MCU)
  128.       ERREXIT(cinfo->emethods, "Sampling factors too large for interleaved scan");
  129.     while (mcublks-- > 0) {
  130.       cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
  131.     }
  132.   }
  133.  
  134.   (*cinfo->methods->d_per_scan_method_selection) (cinfo);
  135. }
  136.  
  137.  
  138. LOCAL void
  139. noninterleaved_scan_setup (decompress_info_ptr cinfo)
  140. /* Compute all derived info for a noninterleaved (single-component) scan */
  141. /* On entry, cinfo->comps_in_scan = 1 and cinfo->cur_comp_info[0] is set up */
  142. {
  143.   jpeg_component_info *compptr = cinfo->cur_comp_info[0];
  144.  
  145.   /* for noninterleaved scan, always one block per MCU */
  146.   compptr->MCU_width = 1;
  147.   compptr->MCU_height = 1;
  148.   compptr->MCU_blocks = 1;
  149.   /* compute physical dimensions of component */
  150.   compptr->downsampled_width = jround_up(compptr->true_comp_width,
  151.                      (long) DCTSIZE);
  152.   compptr->downsampled_height = jround_up(compptr->true_comp_height,
  153.                       (long) DCTSIZE);
  154.  
  155.   cinfo->MCUs_per_row = compptr->downsampled_width / DCTSIZE;
  156.   cinfo->MCU_rows_in_scan = compptr->downsampled_height / DCTSIZE;
  157.  
  158.   /* Prepare array describing MCU composition */
  159.   cinfo->blocks_in_MCU = 1;
  160.   cinfo->MCU_membership[0] = 0;
  161.  
  162.   (*cinfo->methods->d_per_scan_method_selection) (cinfo);
  163. }
  164.  
  165.  
  166.  
  167. LOCAL JSAMPIMAGE
  168. alloc_sampimage (decompress_info_ptr cinfo,
  169.          int num_comps, long num_rows, long num_cols)
  170. /* Allocate an in-memory sample image (all components same size) */
  171. {
  172.   JSAMPIMAGE image;
  173.   int ci;
  174.  
  175.   image = (JSAMPIMAGE) (*cinfo->emethods->alloc_small)
  176.                 (num_comps * SIZEOF(JSAMPARRAY));
  177.   for (ci = 0; ci < num_comps; ci++) {
  178.     image[ci] = (*cinfo->emethods->alloc_small_sarray) (num_cols, num_rows);
  179.   }
  180.   return image;
  181. }
  182.  
  183.  
  184. #if 0                /* this routine not currently needed */
  185.  
  186. LOCAL void
  187. free_sampimage (decompress_info_ptr cinfo, JSAMPIMAGE image, int num_comps)
  188. /* Release a sample image created by alloc_sampimage */
  189. {
  190.   int ci;
  191.  
  192.   for (ci = 0; ci < num_comps; ci++) {
  193.       (*cinfo->emethods->free_small_sarray) (image[ci]);
  194.   }
  195.   (*cinfo->emethods->free_small) ((void *) image);
  196. }
  197.  
  198. #endif
  199.  
  200.  
  201. LOCAL JBLOCKIMAGE
  202. alloc_MCU_row (decompress_info_ptr cinfo)
  203. /* Allocate one MCU row's worth of coefficient blocks */
  204. {
  205.   JBLOCKIMAGE image;
  206.   int ci;
  207.  
  208.   image = (JBLOCKIMAGE) (*cinfo->emethods->alloc_small)
  209.                 (cinfo->comps_in_scan * SIZEOF(JBLOCKARRAY));
  210.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  211.     image[ci] = (*cinfo->emethods->alloc_small_barray)
  212.             (cinfo->cur_comp_info[ci]->downsampled_width / DCTSIZE,
  213.              (long) cinfo->cur_comp_info[ci]->MCU_height);
  214.   }
  215.   return image;
  216. }
  217.  
  218.  
  219. #ifdef NEED_COMPLEX_CONTROLLER    /* not used by simple controller */
  220.  
  221. LOCAL void
  222. free_MCU_row (decompress_info_ptr cinfo, JBLOCKIMAGE image)
  223. /* Release a coefficient block array created by alloc_MCU_row */
  224. {
  225.   int ci;
  226.  
  227.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  228.     (*cinfo->emethods->free_small_barray) (image[ci]);
  229.   }
  230.   (*cinfo->emethods->free_small) ((void *) image);
  231. }
  232.  
  233. #endif
  234.  
  235.  
  236. LOCAL void
  237. alloc_sampling_buffer (decompress_info_ptr cinfo, JSAMPIMAGE sampled_data[2])
  238. /* Create a downsampled-data buffer having the desired structure */
  239. /* (see comments at head of file) */
  240. {
  241.   short ci, vs, i;
  242.  
  243.   /* Get top-level space for array pointers */
  244.   sampled_data[0] = (JSAMPIMAGE) (*cinfo->emethods->alloc_small)
  245.                 (cinfo->comps_in_scan * SIZEOF(JSAMPARRAY));
  246.   sampled_data[1] = (JSAMPIMAGE) (*cinfo->emethods->alloc_small)
  247.                 (cinfo->comps_in_scan * SIZEOF(JSAMPARRAY));
  248.  
  249.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  250.     vs = cinfo->cur_comp_info[ci]->v_samp_factor; /* row group height */
  251.     /* Allocate the real storage */
  252.     sampled_data[0][ci] = (*cinfo->emethods->alloc_small_sarray)
  253.                 (cinfo->cur_comp_info[ci]->downsampled_width,
  254.                 (long) (vs * (DCTSIZE+2)));
  255.     /* Create space for the scrambled-order pointers */
  256.     sampled_data[1][ci] = (JSAMPARRAY) (*cinfo->emethods->alloc_small)
  257.                 (vs * (DCTSIZE+2) * SIZEOF(JSAMPROW));
  258.     /* Duplicate the first DCTSIZE-2 row groups */
  259.     for (i = 0; i < vs * (DCTSIZE-2); i++) {
  260.       sampled_data[1][ci][i] = sampled_data[0][ci][i];
  261.     }
  262.     /* Copy the last four row groups in swapped order */
  263.     for (i = 0; i < vs * 2; i++) {
  264.       sampled_data[1][ci][vs*DCTSIZE + i] = sampled_data[0][ci][vs*(DCTSIZE-2) + i];
  265.       sampled_data[1][ci][vs*(DCTSIZE-2) + i] = sampled_data[0][ci][vs*DCTSIZE + i];
  266.     }
  267.   }
  268. }
  269.  
  270.  
  271. #ifdef NEED_COMPLEX_CONTROLLER    /* not used by simple controller */
  272.  
  273. LOCAL void
  274. free_sampling_buffer (decompress_info_ptr cinfo, JSAMPIMAGE sampled_data[2])
  275. /* Release a sampling buffer created by alloc_sampling_buffer */
  276. {
  277.   short ci;
  278.  
  279.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  280.     /* Free the real storage */
  281.     (*cinfo->emethods->free_small_sarray) (sampled_data[0][ci]);
  282.     /* Free the scrambled-order pointers */
  283.     (*cinfo->emethods->free_small) ((void *) sampled_data[1][ci]);
  284.   }
  285.  
  286.   /* Free the top-level space */
  287.   (*cinfo->emethods->free_small) ((void *) sampled_data[0]);
  288.   (*cinfo->emethods->free_small) ((void *) sampled_data[1]);
  289. }
  290.  
  291. #endif
  292.  
  293.  
  294. /*
  295.  * Several decompression processes need to range-limit values to the range
  296.  * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
  297.  * due to noise introduced by quantization, roundoff error, etc.  These
  298.  * processes are inner loops and need to be as fast as possible.  On most
  299.  * machines, particularly CPUs with pipelines or instruction prefetch,
  300.  * a (range-check-less) C table lookup
  301.  *        x = sample_range_limit[x];
  302.  * is faster than explicit tests
  303.  *        if (x < 0)  x = 0;
  304.  *        else if (x > MAXJSAMPLE)  x = MAXJSAMPLE;
  305.  * These processes all use a common table prepared by the routine below.
  306.  *
  307.  * The table will work correctly for x within MAXJSAMPLE+1 of the legal
  308.  * range.  This is a much wider range than is needed for most cases,
  309.  * but the wide range is handy for color quantization.
  310.  * Note that the table is allocated in near data space on PCs; it's small
  311.  * enough and used often enough to justify this.
  312.  */
  313.  
  314. LOCAL void
  315. prepare_range_limit_table (decompress_info_ptr cinfo)
  316. /* Allocate and fill in the sample_range_limit table */
  317. {
  318.   JSAMPLE * table;
  319.   int i;
  320.  
  321.   table = (JSAMPLE *) (*cinfo->emethods->alloc_small)
  322.             (3 * (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
  323.   cinfo->sample_range_limit = table + (MAXJSAMPLE+1);
  324.   for (i = 0; i <= MAXJSAMPLE; i++) {
  325.     table[i] = 0;            /* sample_range_limit[x] = 0 for x<0 */
  326.     table[i+(MAXJSAMPLE+1)] = (JSAMPLE) i;    /* sample_range_limit[x] = x */
  327.     table[i+(MAXJSAMPLE+1)*2] = MAXJSAMPLE;    /* x beyond MAXJSAMPLE */
  328.   }
  329. }
  330.  
  331.  
  332. LOCAL void
  333. duplicate_row (JSAMPARRAY image_data,
  334.            long num_cols, int source_row, int num_rows)
  335. /* Duplicate the source_row at source_row+1 .. source_row+num_rows */
  336. /* This happens only at the bottom of the image, */
  337. /* so it needn't be super-efficient */
  338. {
  339.   register int row;
  340.  
  341.   for (row = 1; row <= num_rows; row++) {
  342.     jcopy_sample_rows(image_data, source_row, image_data, source_row + row,
  343.               1, num_cols);
  344.   }
  345. }
  346.  
  347.  
  348. LOCAL void
  349. expand (decompress_info_ptr cinfo,
  350.     JSAMPIMAGE sampled_data, JSAMPIMAGE fullsize_data,
  351.     long fullsize_width,
  352.     short above, short current, short below, short out)
  353. /* Do upsampling expansion of a single row group (of each component). */
  354. /* above, current, below are indexes of row groups in sampled_data;       */
  355. /* out is the index of the target row group in fullsize_data.             */
  356. /* Special case: above, below can be -1 to indicate top, bottom of image. */
  357. {
  358.   jpeg_component_info *compptr;
  359.   JSAMPARRAY above_ptr, below_ptr;
  360.   JSAMPROW dummy[MAX_SAMP_FACTOR]; /* for downsample expansion at top/bottom */
  361.   short ci, vs, i;
  362.  
  363.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  364.     compptr = cinfo->cur_comp_info[ci];
  365.     vs = compptr->v_samp_factor; /* row group height */
  366.  
  367.     if (above >= 0)
  368.       above_ptr = sampled_data[ci] + above * vs;
  369.     else {
  370.       /* Top of image: make a dummy above-context with copies of 1st row */
  371.       /* We assume current=0 in this case */
  372.       for (i = 0; i < vs; i++)
  373.     dummy[i] = sampled_data[ci][0];
  374.       above_ptr = (JSAMPARRAY) dummy; /* possible near->far pointer conv */
  375.     }
  376.  
  377.     if (below >= 0)
  378.       below_ptr = sampled_data[ci] + below * vs;
  379.     else {
  380.       /* Bot of image: make a dummy below-context with copies of last row */
  381.       for (i = 0; i < vs; i++)
  382.     dummy[i] = sampled_data[ci][(current+1)*vs-1];
  383.       below_ptr = (JSAMPARRAY) dummy; /* possible near->far pointer conv */
  384.     }
  385.  
  386.     (*cinfo->methods->upsample[ci])
  387.         (cinfo, (int) ci,
  388.          compptr->downsampled_width, (int) vs,
  389.          fullsize_width, (int) cinfo->max_v_samp_factor,
  390.          above_ptr,
  391.          sampled_data[ci] + current * vs,
  392.          below_ptr,
  393.          fullsize_data[ci] + out * cinfo->max_v_samp_factor);
  394.   }
  395. }
  396.  
  397.  
  398. LOCAL void
  399. emit_1pass (decompress_info_ptr cinfo, int num_rows, JSAMPIMAGE fullsize_data,
  400.         JSAMPARRAY dummy)
  401. /* Do color processing and output of num_rows full-size rows. */
  402. /* This is not used when doing 2-pass color quantization. */
  403. /* The dummy argument simply lets this be called via scan_big_image. */
  404. {
  405.   if (cinfo->quantize_colors) {
  406.     (*cinfo->methods->color_quantize) (cinfo, num_rows, fullsize_data,
  407.                        output_workspace[0]);
  408.   } else {
  409.     (*cinfo->methods->color_convert) (cinfo, num_rows, cinfo->image_width,
  410.                       fullsize_data, output_workspace);
  411.   }
  412.     
  413.   (*cinfo->methods->put_pixel_rows) (cinfo, num_rows, output_workspace);
  414. }
  415.  
  416.  
  417. /*
  418.  * Support routines for complex controller.
  419.  */
  420.  
  421. #ifdef NEED_COMPLEX_CONTROLLER
  422.  
  423. METHODDEF void
  424. scan_big_image (decompress_info_ptr cinfo, quantize_method_ptr quantize_method)
  425. /* Apply quantize_method to entire image stored in fullsize_image[]. */
  426. /* This is the "iterator" routine used by the 2-pass color quantizer. */
  427. /* We also use it directly in some cases. */
  428. {
  429.   long pixel_rows_output;
  430.   short ci;
  431.  
  432.   for (pixel_rows_output = 0; pixel_rows_output < cinfo->image_height;
  433.        pixel_rows_output += rows_in_mem) {
  434.     (*cinfo->methods->progress_monitor) (cinfo, pixel_rows_output,
  435.                      cinfo->image_height);
  436.     /* Realign the big buffers */
  437.     for (ci = 0; ci < cinfo->num_components; ci++) {
  438.       fullsize_ptrs[ci] = (*cinfo->emethods->access_big_sarray)
  439.     (fullsize_image[ci], pixel_rows_output, FALSE);
  440.     }
  441.     /* Let the quantizer have its way with the data.
  442.      * Note that output_workspace is simply workspace for the quantizer;
  443.      * when it's ready to output, it must call put_pixel_rows itself.
  444.      */
  445.     (*quantize_method) (cinfo,
  446.             (int) MIN((long) rows_in_mem,
  447.                   cinfo->image_height - pixel_rows_output),
  448.             fullsize_ptrs, output_workspace[0]);
  449.   }
  450.  
  451.   cinfo->completed_passes++;
  452. }
  453.  
  454. #endif /* NEED_COMPLEX_CONTROLLER */
  455.  
  456.  
  457. /*
  458.  * Support routines for cross-block smoothing.
  459.  */
  460.  
  461. #ifdef BLOCK_SMOOTHING_SUPPORTED
  462.  
  463.  
  464. LOCAL void
  465. smooth_mcu_row (decompress_info_ptr cinfo,
  466.         JBLOCKIMAGE above, JBLOCKIMAGE input, JBLOCKIMAGE below,
  467.         JBLOCKIMAGE output)
  468. /* Apply cross-block smoothing to one MCU row's worth of coefficient blocks. */
  469. /* above,below are NULL if at top/bottom of image. */
  470. {
  471.   jpeg_component_info *compptr;
  472.   short ci, ri, last;
  473.   JBLOCKROW prev;
  474.  
  475.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  476.     compptr = cinfo->cur_comp_info[ci];
  477.     last = compptr->MCU_height - 1;
  478.  
  479.     if (above == NULL)
  480.       prev = NULL;
  481.     else
  482.       prev = above[ci][last];
  483.  
  484.     for (ri = 0; ri < last; ri++) {
  485.       (*cinfo->methods->smooth_coefficients) (cinfo, compptr,
  486.                 prev, input[ci][ri], input[ci][ri+1],
  487.                 output[ci][ri]);
  488.       prev = input[ci][ri];
  489.     }
  490.  
  491.     if (below == NULL)
  492.       (*cinfo->methods->smooth_coefficients) (cinfo, compptr,
  493.                 prev, input[ci][last], (JBLOCKROW) NULL,
  494.                 output[ci][last]);
  495.     else
  496.       (*cinfo->methods->smooth_coefficients) (cinfo, compptr,
  497.                 prev, input[ci][last], below[ci][0],
  498.                 output[ci][last]);
  499.   }
  500. }
  501.  
  502.  
  503. LOCAL void
  504. get_smoothed_row (decompress_info_ptr cinfo, JBLOCKIMAGE coeff_data,
  505.           JBLOCKIMAGE bsmooth[3], int * whichb, long cur_mcu_row)
  506. /* Get an MCU row of coefficients, applying cross-block smoothing. */
  507. /* The output row is placed in coeff_data.  bsmooth and whichb hold */
  508. /* working state, and cur_row is needed to check for image top/bottom. */
  509. /* This routine just takes care of the buffering logic. */
  510. {
  511.   int prev, cur, next;
  512.   
  513.   /* Special case for top of image: need to pre-fetch a row & init whichb */
  514.   if (cur_mcu_row == 0) {
  515.     (*cinfo->methods->disassemble_MCU) (cinfo, bsmooth[0]);
  516.     if (cinfo->MCU_rows_in_scan > 1) {
  517.       (*cinfo->methods->disassemble_MCU) (cinfo, bsmooth[1]);
  518.       smooth_mcu_row(cinfo, (JBLOCKIMAGE) NULL, bsmooth[0], bsmooth[1],
  519.              coeff_data);
  520.     } else {
  521.       smooth_mcu_row(cinfo, (JBLOCKIMAGE) NULL, bsmooth[0], (JBLOCKIMAGE) NULL,
  522.              coeff_data);
  523.     }
  524.     *whichb = 1;        /* points to next bsmooth[] element to use */
  525.     return;
  526.   }
  527.   
  528.   cur = *whichb;        /* set up references */
  529.   prev = (cur == 0 ? 2 : cur - 1);
  530.   next = (cur == 2 ? 0 : cur + 1);
  531.   *whichb = next;        /* advance whichb for next time */
  532.   
  533.   /* Special case for bottom of image: don't read another row */
  534.   if (cur_mcu_row >= cinfo->MCU_rows_in_scan - 1) {
  535.     smooth_mcu_row(cinfo, bsmooth[prev], bsmooth[cur], (JBLOCKIMAGE) NULL,
  536.            coeff_data);
  537.     return;
  538.   }
  539.   
  540.   /* Normal case: read ahead a new row, smooth the one I got before */
  541.   (*cinfo->methods->disassemble_MCU) (cinfo, bsmooth[next]);
  542.   smooth_mcu_row(cinfo, bsmooth[prev], bsmooth[cur], bsmooth[next],
  543.          coeff_data);
  544. }
  545.  
  546.  
  547. #endif /* BLOCK_SMOOTHING_SUPPORTED */
  548.  
  549.  
  550.  
  551. /*
  552.  * Decompression pipeline controller used for single-scan files
  553.  * without 2-pass color quantization.
  554.  */
  555.  
  556. METHODDEF void
  557. simple_dcontroller (decompress_info_ptr cinfo)
  558. {
  559.   long fullsize_width;        /* # of samples per row in full-size buffers */
  560.   long cur_mcu_row;        /* counts # of MCU rows processed */
  561.   long pixel_rows_output;    /* # of pixel rows actually emitted */
  562.   int mcu_rows_per_loop;    /* # of MCU rows processed per outer loop */
  563.   /* Work buffer for dequantized coefficients (IDCT input) */
  564.   JBLOCKIMAGE coeff_data;
  565.   /* Work buffer for cross-block smoothing input */
  566. #ifdef BLOCK_SMOOTHING_SUPPORTED
  567.   JBLOCKIMAGE bsmooth[3];    /* this is optional */
  568.   int whichb;
  569. #endif
  570.   /* Work buffer for downsampled image data (see comments at head of file) */
  571.   JSAMPIMAGE sampled_data[2];
  572.   /* Work buffer for upsampled data */
  573.   JSAMPIMAGE fullsize_data;
  574.   int whichss, ri;
  575.   short i;
  576.  
  577.   /* Compute dimensions of full-size pixel buffers */
  578.   /* Note these are the same whether interleaved or not. */
  579.   rows_in_mem = cinfo->max_v_samp_factor * DCTSIZE;
  580.   fullsize_width = jround_up(cinfo->image_width,
  581.                  (long) (cinfo->max_h_samp_factor * DCTSIZE));
  582.  
  583.   /* Prepare for single scan containing all components */
  584.   if (cinfo->comps_in_scan == 1) {
  585.     noninterleaved_scan_setup(cinfo);
  586.     /* Need to read Vk MCU rows to obtain Vk block rows */
  587.     mcu_rows_per_loop = cinfo->cur_comp_info[0]->v_samp_factor;
  588.   } else {
  589.     interleaved_scan_setup(cinfo);
  590.     /* in an interleaved scan, one MCU row provides Vk block rows */
  591.     mcu_rows_per_loop = 1;
  592.   }
  593.   cinfo->total_passes++;
  594.  
  595.   /* Allocate working memory: */
  596.   /* coeff_data holds a single MCU row of coefficient blocks */
  597.   coeff_data = alloc_MCU_row(cinfo);
  598.   /* if doing cross-block smoothing, need extra space for its input */
  599. #ifdef BLOCK_SMOOTHING_SUPPORTED
  600.   if (cinfo->do_block_smoothing) {
  601.     bsmooth[0] = alloc_MCU_row(cinfo);
  602.     bsmooth[1] = alloc_MCU_row(cinfo);
  603.     bsmooth[2] = alloc_MCU_row(cinfo);
  604.   }
  605. #endif
  606.   /* sampled_data is sample data before upsampling */
  607.   alloc_sampling_buffer(cinfo, sampled_data);
  608.   /* fullsize_data is sample data after upsampling */
  609.   fullsize_data = alloc_sampimage(cinfo, (int) cinfo->num_components,
  610.                   (long) rows_in_mem, fullsize_width);
  611.   /* output_workspace is the color-processed data */
  612.   output_workspace = alloc_sampimage(cinfo, (int) cinfo->final_out_comps,
  613.                      (long) rows_in_mem, fullsize_width);
  614.   prepare_range_limit_table(cinfo);
  615.  
  616.   /* Tell the memory manager to instantiate big arrays.
  617.    * We don't need any big arrays in this controller,
  618.    * but some other module (like the output file writer) may need one.
  619.    */
  620.   (*cinfo->emethods->alloc_big_arrays)
  621.     ((long) 0,                /* no more small sarrays */
  622.      (long) 0,                /* no more small barrays */
  623.      (long) 0);                /* no more "medium" objects */
  624.   /* NB: if quantizer needs any "medium" size objects, it must get them */
  625.   /* at color_quant_init time */
  626.  
  627.   /* Initialize to read scan data */
  628.  
  629.   (*cinfo->methods->entropy_decode_init) (cinfo);
  630.   (*cinfo->methods->upsample_init) (cinfo);
  631.   (*cinfo->methods->disassemble_init) (cinfo);
  632.  
  633.   /* Loop over scan's data: rows_in_mem pixel rows are processed per loop */
  634.  
  635.   pixel_rows_output = 0;
  636.   whichss = 1;            /* arrange to start with sampled_data[0] */
  637.  
  638.   for (cur_mcu_row = 0; cur_mcu_row < cinfo->MCU_rows_in_scan;
  639.        cur_mcu_row += mcu_rows_per_loop) {
  640.     (*cinfo->methods->progress_monitor) (cinfo, cur_mcu_row,
  641.                      cinfo->MCU_rows_in_scan);
  642.  
  643.     whichss ^= 1;        /* switch to other downsampled-data buffer */
  644.  
  645.     /* Obtain v_samp_factor block rows of each component in the scan. */
  646.     /* This is a single MCU row if interleaved, multiple MCU rows if not. */
  647.     /* In the noninterleaved case there might be fewer than v_samp_factor */
  648.     /* block rows remaining; if so, pad with copies of the last pixel row */
  649.     /* so that upsampling doesn't have to treat it as a special case. */
  650.  
  651.     for (ri = 0; ri < mcu_rows_per_loop; ri++) {
  652.       if (cur_mcu_row + ri < cinfo->MCU_rows_in_scan) {
  653.     /* OK to actually read an MCU row. */
  654. #ifdef BLOCK_SMOOTHING_SUPPORTED
  655.     if (cinfo->do_block_smoothing)
  656.       get_smoothed_row(cinfo, coeff_data,
  657.                bsmooth, &whichb, cur_mcu_row + ri);
  658.     else
  659. #endif
  660.       (*cinfo->methods->disassemble_MCU) (cinfo, coeff_data);
  661.       
  662.     (*cinfo->methods->reverse_DCT) (cinfo, coeff_data,
  663.                     sampled_data[whichss],
  664.                     ri * DCTSIZE);
  665.       } else {
  666.     /* Need to pad out with copies of the last downsampled row. */
  667.     /* This can only happen if there is just one component. */
  668.     duplicate_row(sampled_data[whichss][0],
  669.               cinfo->cur_comp_info[0]->downsampled_width,
  670.               ri * DCTSIZE - 1, DCTSIZE);
  671.       }
  672.     }
  673.  
  674.     /* Upsample the data */
  675.     /* First time through is a special case */
  676.  
  677.     if (cur_mcu_row) {
  678.       /* Expand last row group of previous set */
  679.       expand(cinfo, sampled_data[whichss], fullsize_data, fullsize_width,
  680.          (short) DCTSIZE, (short) (DCTSIZE+1), (short) 0,
  681.          (short) (DCTSIZE-1));
  682.       /* and dump the previous set's expanded data */
  683.       emit_1pass (cinfo, rows_in_mem, fullsize_data, (JSAMPARRAY) NULL);
  684.       pixel_rows_output += rows_in_mem;
  685.       /* Expand first row group of this set */
  686.       expand(cinfo, sampled_data[whichss], fullsize_data, fullsize_width,
  687.          (short) (DCTSIZE+1), (short) 0, (short) 1,
  688.          (short) 0);
  689.     } else {
  690.       /* Expand first row group with dummy above-context */
  691.       expand(cinfo, sampled_data[whichss], fullsize_data, fullsize_width,
  692.          (short) (-1), (short) 0, (short) 1,
  693.          (short) 0);
  694.     }
  695.     /* Expand second through next-to-last row groups of this set */
  696.     for (i = 1; i <= DCTSIZE-2; i++) {
  697.       expand(cinfo, sampled_data[whichss], fullsize_data, fullsize_width,
  698.          (short) (i-1), (short) i, (short) (i+1),
  699.          (short) i);
  700.     }
  701.   } /* end of outer loop */
  702.  
  703.   /* Expand the last row group with dummy below-context */
  704.   /* Note whichss points to last buffer side used */
  705.   expand(cinfo, sampled_data[whichss], fullsize_data, fullsize_width,
  706.      (short) (DCTSIZE-2), (short) (DCTSIZE-1), (short) (-1),
  707.      (short) (DCTSIZE-1));
  708.   /* and dump the remaining data (may be less than full height) */
  709.   emit_1pass (cinfo, (int) (cinfo->image_height - pixel_rows_output),
  710.           fullsize_data, (JSAMPARRAY) NULL);
  711.  
  712.   /* Clean up after the scan */
  713.   (*cinfo->methods->disassemble_term) (cinfo);
  714.   (*cinfo->methods->upsample_term) (cinfo);
  715.   (*cinfo->methods->entropy_decode_term) (cinfo);
  716.   (*cinfo->methods->read_scan_trailer) (cinfo);
  717.   cinfo->completed_passes++;
  718. #ifdef    STREAM_IMAGE
  719.   { extern    should_be;
  720. #ifdef    OLD_STREAM_IMAGE
  721.     ri = ftell(cinfo->input_file);
  722.     if (should_be > 0 && ri != should_be) {
  723. # ifdef _DEBUG_
  724.     fprintf(stderr, "cur_pos %d, should be %d\n", ri, should_be);
  725. # endif
  726.     if (ri > 0)
  727.     (*cinfo->img->seek)(cinfo->input_file, should_be - ri, SEEK_CUR);
  728. # ifdef    NEW_FILE_SYSTEM
  729.     else    while (1) {
  730.         ri = (*cinfo->img->seek)(cinfo->input_file, 0, SEEK_GETB);
  731.         if (ri == 0xFF && 0xDA ==
  732.             (*cinfo->img->seek)(cinfo->input_file, 0, SEEK_PEEK)) {
  733.             (*cinfo->img->seek)(cinfo->input_file, 0xda, SEEK_UGETB);
  734.             (*cinfo->img->seek)(cinfo->input_file, ri, SEEK_UGETB);
  735.             break;
  736.         }
  737.     }
  738. # else
  739.     else while (1)    {
  740.         ri = JGETC(cinfo);
  741.         if (ri == 0xFF && JGETC(cinfo) == 0xDA)    {
  742.             JUNGETC(0xDA, cinfo);
  743.             JUNGETC(ri, cinfo);
  744.             break;
  745.         }
  746.     }
  747. # endif
  748.     } else    fprintf(stderr, "even ending\n");
  749. #else
  750.     if (should_be >= ftell(cinfo->input_file))
  751.     cinfo->bytes_in_buffer = 0;    /* flush rest garbage    */
  752. #endif
  753.   }
  754. #endif
  755.  
  756.   /* Verify that we've seen the whole input file */
  757.   if ((*cinfo->methods->read_scan_header) (cinfo))
  758.     WARNMS(cinfo->emethods, "Multi_Frames");
  759.  
  760.   /* Release working memory */
  761.   /* (no work -- we let free_all release what's needful) */
  762. }
  763.  
  764.  
  765. /*
  766.  * Decompression pipeline controller used for multiple-scan files
  767.  * and/or 2-pass color quantization.
  768.  *
  769.  * The current implementation places the "big" buffer at the stage of
  770.  * upsampled, non-color-processed data.  This is the only place that
  771.  * makes sense when doing 2-pass quantization.  For processing multiple-scan
  772.  * files without 2-pass quantization, it would be possible to develop another
  773.  * controller that buffers the downsampled data instead, thus reducing the size
  774.  * of the temp files (by about a factor of 2 in typical cases).  However,
  775.  * our present upsampling logic is dependent on the assumption that
  776.  * upsampling occurs during a scan, so it's much easier to do the
  777.  * enlargement as the JPEG file is read.  This also simplifies life for the
  778.  * memory manager, which would otherwise have to deal with overlapping
  779.  * access_big_sarray() requests.
  780.  * At present it appears that most JPEG files will be single-scan,
  781.  * so it doesn't seem worthwhile to worry about this optimization.
  782.  */
  783.  
  784. #ifdef NEED_COMPLEX_CONTROLLER
  785.  
  786. METHODDEF void
  787. complex_dcontroller (decompress_info_ptr cinfo)
  788. {
  789.   long fullsize_width;        /* # of samples per row in full-size buffers */
  790.   long cur_mcu_row;        /* counts # of MCU rows processed */
  791.   long pixel_rows_output;    /* # of pixel rows actually emitted */
  792.   int mcu_rows_per_loop;    /* # of MCU rows processed per outer loop */
  793.   /* Work buffer for dequantized coefficients (IDCT input) */
  794.   JBLOCKIMAGE coeff_data;
  795.   /* Work buffer for cross-block smoothing input */
  796. #ifdef BLOCK_SMOOTHING_SUPPORTED
  797.   JBLOCKIMAGE bsmooth[3];    /* this is optional */
  798.   int whichb;
  799. #endif
  800.   /* Work buffer for downsampled image data (see comments at head of file) */
  801.   JSAMPIMAGE sampled_data[2];
  802.   int whichss, ri;
  803.   short ci, i;
  804.   boolean single_scan;
  805.  
  806.   /* Compute dimensions of full-size pixel buffers */
  807.   /* Note these are the same whether interleaved or not. */
  808.   rows_in_mem = cinfo->max_v_samp_factor * DCTSIZE;
  809.   fullsize_width = jround_up(cinfo->image_width,
  810.                  (long) (cinfo->max_h_samp_factor * DCTSIZE));
  811.  
  812.   /* Allocate all working memory that doesn't depend on scan info */
  813.   /* output_workspace is the color-processed data */
  814.   output_workspace = alloc_sampimage(cinfo, (int) cinfo->final_out_comps,
  815.                      (long) rows_in_mem, fullsize_width);
  816.   prepare_range_limit_table(cinfo);
  817.  
  818.   /* Get a big image: fullsize_image is sample data after upsampling. */
  819.   fullsize_image = (big_sarray_ptr *) (*cinfo->emethods->alloc_small)
  820.             (cinfo->num_components * SIZEOF(big_sarray_ptr));
  821.   for (ci = 0; ci < cinfo->num_components; ci++) {
  822.     fullsize_image[ci] = (*cinfo->emethods->request_big_sarray)
  823.             (fullsize_width,
  824.              jround_up(cinfo->image_height, (long) rows_in_mem),
  825.              (long) rows_in_mem);
  826.   }
  827.   /* Also get an area for pointers to currently accessible chunks */
  828.   fullsize_ptrs = (JSAMPIMAGE) (*cinfo->emethods->alloc_small)
  829.                 (cinfo->num_components * SIZEOF(JSAMPARRAY));
  830.  
  831.   /* Tell the memory manager to instantiate big arrays */
  832.   (*cinfo->emethods->alloc_big_arrays)
  833.      /* extra sarray space is for downsampled-data buffers: */
  834.     ((long) (fullsize_width            /* max width in samples */
  835.      * cinfo->max_v_samp_factor*(DCTSIZE+2)    /* max height */
  836.      * cinfo->num_components),        /* max components per scan */
  837.      /* extra barray space is for MCU-row buffers: */
  838.      (long) ((fullsize_width / DCTSIZE)    /* max width in blocks */
  839.      * cinfo->max_v_samp_factor        /* max height */
  840.      * cinfo->num_components        /* max components per scan */
  841.      * (cinfo->do_block_smoothing ? 4 : 1)),/* how many of these we need */
  842.      /* no extra "medium"-object space */
  843.      (long) 0);
  844.   /* NB: if quantizer needs any "medium" size objects, it must get them */
  845.   /* at color_quant_init time */
  846.  
  847.   /* If file is single-scan, we can do color quantization prescan on-the-fly
  848.    * during the scan (we must be doing 2-pass quantization, else this method
  849.    * would not have been selected).  If it is multiple scans, we have to make
  850.    * a separate pass after we've collected all the components.  (We could save
  851.    * some I/O by doing CQ prescan during the last scan, but the extra logic
  852.    * doesn't seem worth the trouble.)
  853.    */
  854.  
  855.   single_scan = (cinfo->comps_in_scan == cinfo->num_components);
  856.  
  857.   /* Account for passes needed (color quantizer adds its passes separately).
  858.    * If multiscan file, we guess that each component has its own scan,
  859.    * and increment completed_passes by the number of components in the scan.
  860.    */
  861.  
  862.   if (single_scan)
  863.     cinfo->total_passes++;    /* the single scan */
  864.   else {
  865.     cinfo->total_passes += cinfo->num_components; /* guessed # of scans */
  866.     if (cinfo->two_pass_quantize)
  867.       cinfo->total_passes++;    /* account for separate CQ prescan pass */
  868.   }
  869.   if (! cinfo->two_pass_quantize)
  870.     cinfo->total_passes++;    /* count output pass unless quantizer does it */
  871.  
  872.   /* Loop over scans in file */
  873.  
  874.   do {
  875.     
  876.     /* Prepare for this scan */
  877.     if (cinfo->comps_in_scan == 1) {
  878.       noninterleaved_scan_setup(cinfo);
  879.       /* Need to read Vk MCU rows to obtain Vk block rows */
  880.       mcu_rows_per_loop = cinfo->cur_comp_info[0]->v_samp_factor;
  881.     } else {
  882.       interleaved_scan_setup(cinfo);
  883.       /* in an interleaved scan, one MCU row provides Vk block rows */
  884.       mcu_rows_per_loop = 1;
  885.     }
  886.     
  887.     /* Allocate scan-local working memory */
  888.     /* coeff_data holds a single MCU row of coefficient blocks */
  889.     coeff_data = alloc_MCU_row(cinfo);
  890.     /* if doing cross-block smoothing, need extra space for its input */
  891. #ifdef BLOCK_SMOOTHING_SUPPORTED
  892.     if (cinfo->do_block_smoothing) {
  893.       bsmooth[0] = alloc_MCU_row(cinfo);
  894.       bsmooth[1] = alloc_MCU_row(cinfo);
  895.       bsmooth[2] = alloc_MCU_row(cinfo);
  896.     }
  897. #endif
  898.     /* sampled_data is sample data before upsampling */
  899.     alloc_sampling_buffer(cinfo, sampled_data);
  900.  
  901.     /* line up the big buffers for components in this scan */
  902.     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  903.       fullsize_ptrs[ci] = (*cinfo->emethods->access_big_sarray)
  904.     (fullsize_image[cinfo->cur_comp_info[ci]->component_index],
  905.      (long) 0, TRUE);
  906.     }
  907.     
  908.     /* Initialize to read scan data */
  909.     
  910.     (*cinfo->methods->entropy_decode_init) (cinfo);
  911.     (*cinfo->methods->upsample_init) (cinfo);
  912.     (*cinfo->methods->disassemble_init) (cinfo);
  913.     
  914.     /* Loop over scan's data: rows_in_mem pixel rows are processed per loop */
  915.     
  916.     pixel_rows_output = 0;
  917.     whichss = 1;        /* arrange to start with sampled_data[0] */
  918.     
  919.     for (cur_mcu_row = 0; cur_mcu_row < cinfo->MCU_rows_in_scan;
  920.      cur_mcu_row += mcu_rows_per_loop) {
  921.       (*cinfo->methods->progress_monitor) (cinfo, cur_mcu_row,
  922.                        cinfo->MCU_rows_in_scan);
  923.  
  924.       whichss ^= 1;        /* switch to other downsampled-data buffer */
  925.  
  926.       /* Obtain v_samp_factor block rows of each component in the scan. */
  927.       /* This is a single MCU row if interleaved, multiple MCU rows if not. */
  928.       /* In the noninterleaved case there might be fewer than v_samp_factor */
  929.       /* block rows remaining; if so, pad with copies of the last pixel row */
  930.       /* so that upsampling doesn't have to treat it as a special case. */
  931.       
  932.       for (ri = 0; ri < mcu_rows_per_loop; ri++) {
  933.     if (cur_mcu_row + ri < cinfo->MCU_rows_in_scan) {
  934.       /* OK to actually read an MCU row. */
  935. #ifdef BLOCK_SMOOTHING_SUPPORTED
  936.       if (cinfo->do_block_smoothing)
  937.         get_smoothed_row(cinfo, coeff_data,
  938.                  bsmooth, &whichb, cur_mcu_row + ri);
  939.       else
  940. #endif
  941.         (*cinfo->methods->disassemble_MCU) (cinfo, coeff_data);
  942.       
  943.       (*cinfo->methods->reverse_DCT) (cinfo, coeff_data,
  944.                       sampled_data[whichss],
  945.                       ri * DCTSIZE);
  946.     } else {
  947.       /* Need to pad out with copies of the last downsampled row. */
  948.       /* This can only happen if there is just one component. */
  949.       duplicate_row(sampled_data[whichss][0],
  950.             cinfo->cur_comp_info[0]->downsampled_width,
  951.             ri * DCTSIZE - 1, DCTSIZE);
  952.     }
  953.       }
  954.       
  955.       /* Upsample the data */
  956.       /* First time through is a special case */
  957.       
  958.       if (cur_mcu_row) {
  959.     /* Expand last row group of previous set */
  960.     expand(cinfo, sampled_data[whichss], fullsize_ptrs, fullsize_width,
  961.            (short) DCTSIZE, (short) (DCTSIZE+1), (short) 0,
  962.            (short) (DCTSIZE-1));
  963.     /* If single scan, can do color quantization prescan on-the-fly */
  964.     if (single_scan)
  965.       (*cinfo->methods->color_quant_prescan) (cinfo, rows_in_mem,
  966.                           fullsize_ptrs,
  967.                           output_workspace[0]);
  968.     /* Realign the big buffers */
  969.     pixel_rows_output += rows_in_mem;
  970.     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  971.       fullsize_ptrs[ci] = (*cinfo->emethods->access_big_sarray)
  972.         (fullsize_image[cinfo->cur_comp_info[ci]->component_index],
  973.          pixel_rows_output, TRUE);
  974.     }
  975.     /* Expand first row group of this set */
  976.     expand(cinfo, sampled_data[whichss], fullsize_ptrs, fullsize_width,
  977.            (short) (DCTSIZE+1), (short) 0, (short) 1,
  978.            (short) 0);
  979.       } else {
  980.     /* Expand first row group with dummy above-context */
  981.     expand(cinfo, sampled_data[whichss], fullsize_ptrs, fullsize_width,
  982.            (short) (-1), (short) 0, (short) 1,
  983.            (short) 0);
  984.       }
  985.       /* Expand second through next-to-last row groups of this set */
  986.       for (i = 1; i <= DCTSIZE-2; i++) {
  987.     expand(cinfo, sampled_data[whichss], fullsize_ptrs, fullsize_width,
  988.            (short) (i-1), (short) i, (short) (i+1),
  989.            (short) i);
  990.       }
  991.     } /* end of loop over scan's data */
  992.     
  993.     /* Expand the last row group with dummy below-context */
  994.     /* Note whichss points to last buffer side used */
  995.     expand(cinfo, sampled_data[whichss], fullsize_ptrs, fullsize_width,
  996.        (short) (DCTSIZE-2), (short) (DCTSIZE-1), (short) (-1),
  997.        (short) (DCTSIZE-1));
  998.     /* If single scan, finish on-the-fly color quantization prescan */
  999.     if (single_scan)
  1000.       (*cinfo->methods->color_quant_prescan) (cinfo,
  1001.             (int) (cinfo->image_height - pixel_rows_output),
  1002.             fullsize_ptrs, output_workspace[0]);
  1003.     
  1004.     /* Clean up after the scan */
  1005.     (*cinfo->methods->disassemble_term) (cinfo);
  1006.     (*cinfo->methods->upsample_term) (cinfo);
  1007.     (*cinfo->methods->entropy_decode_term) (cinfo);
  1008.     (*cinfo->methods->read_scan_trailer) (cinfo);
  1009.     if (single_scan)
  1010.       cinfo->completed_passes++;
  1011.     else
  1012.       cinfo->completed_passes += cinfo->comps_in_scan;
  1013.  
  1014.     /* Release scan-local working memory */
  1015.     free_MCU_row(cinfo, coeff_data);
  1016. #ifdef BLOCK_SMOOTHING_SUPPORTED
  1017.     if (cinfo->do_block_smoothing) {
  1018.       free_MCU_row(cinfo, bsmooth[0]);
  1019.       free_MCU_row(cinfo, bsmooth[1]);
  1020.       free_MCU_row(cinfo, bsmooth[2]);
  1021.     }
  1022. #endif
  1023.     free_sampling_buffer(cinfo, sampled_data);
  1024.     
  1025.     /* Repeat if there is another scan */
  1026.   } while ((!single_scan) && (*cinfo->methods->read_scan_header) (cinfo));
  1027.  
  1028.   if (single_scan) {
  1029.     /* If we expected just one scan, make SURE there's just one */
  1030.     if ((*cinfo->methods->read_scan_header) (cinfo))
  1031.       WARNMS(cinfo->emethods, "multi_frame");
  1032. /*      WARNMS(cinfo->emethods, "Didn't expect more than one scan");    */
  1033.     /* We did the CQ prescan on-the-fly, so we are all set. */
  1034.   } else {
  1035.     /* For multiple-scan file, do the CQ prescan as a separate pass. */
  1036.     /* The main reason why prescan is passed the output_workspace is */
  1037.     /* so that we can use scan_big_image to call it... */
  1038.     if (cinfo->two_pass_quantize)
  1039.       scan_big_image(cinfo, cinfo->methods->color_quant_prescan);
  1040.   }
  1041.  
  1042.   /* Now that we've collected the data, do color processing and output */
  1043.   if (cinfo->two_pass_quantize)
  1044.     (*cinfo->methods->color_quant_doit) (cinfo, scan_big_image);
  1045.   else
  1046.     scan_big_image(cinfo, emit_1pass);
  1047.  
  1048.   /* Release working memory */
  1049.   /* (no work -- we let free_all release what's needful) */
  1050. }
  1051.  
  1052. #endif /* NEED_COMPLEX_CONTROLLER */
  1053.  
  1054.  
  1055. /*
  1056.  * The method selection routine for decompression pipeline controllers.
  1057.  * Note that at this point we've already read the JPEG header and first SOS,
  1058.  * so we can tell whether the input is one scan or not.
  1059.  */
  1060.  
  1061. GLOBAL void
  1062. jseldpipeline (decompress_info_ptr cinfo)
  1063. {
  1064.   /* simplify subsequent tests on color quantization */
  1065.   if (! cinfo->quantize_colors)
  1066.     cinfo->two_pass_quantize = FALSE;
  1067.   
  1068.   if (cinfo->comps_in_scan == cinfo->num_components) {
  1069.     /* It's a single-scan file */
  1070.     if (cinfo->two_pass_quantize) {
  1071. #ifdef NEED_COMPLEX_CONTROLLER
  1072.       cinfo->methods->d_pipeline_controller = complex_dcontroller;
  1073. #else
  1074.       ERREXIT(cinfo->emethods, "2-pass quantization support was not compiled");
  1075. #endif
  1076.     } else
  1077.       cinfo->methods->d_pipeline_controller = simple_dcontroller;
  1078.   } else {
  1079.     /* It's a multiple-scan file */
  1080. #ifdef NEED_COMPLEX_CONTROLLER
  1081.     cinfo->methods->d_pipeline_controller = complex_dcontroller;
  1082. #else
  1083.     ERREXIT(cinfo->emethods, "Multiple-scan support was not compiled");
  1084. #endif
  1085.   }
  1086. }
  1087.